package com.xinyun.server.thread;

import com.xinyun.server.ChannelManager;
import com.xinyun.server.Session;
import com.xinyun.server.protocol.send.FindBoardId;
import com.xinyun.server.protocol.send.ScanAll;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;

/**
 * Created by bubusy on 16/5/22.
 */
public class CheckBoardIdThread extends Thread {

    private Log log = LogFactory.getLog(CheckBoardIdThread.class);

    private ChannelFuture channelFuture;


    public CheckBoardIdThread(ChannelFuture channelFuture) {

        this.channelFuture = channelFuture;
    }

    @Override
    public void run() {

        try {
            while (true) {
                Thread.sleep(5000);
                log.debug("检查socket链接上是否已绑定boardId");
                Map<String, Session> channels = ChannelManager.getChannels();
                log.debug("当前在线链接: " + channels.size());
                for (Session s : channels.values()) {
                    String boardId = s.boardId;
                    if (boardId == null) {
                        Channel channel = s.channel;
                        channel.writeAndFlush(new FindBoardId());
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
